﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace POSPlus.EZBarTender
{
    /// <summary>
    /// Simple extension of the DataGridView to provide a background image.
    /// </summary>
    public class BackgroundableDataGridView : DataGridView
    {
        // The background image
        private Image _image;

        #region Constructors
        /// <summary>
        /// Default constructor generated by Visual Studio 2010.
        /// </summary>
        public BackgroundableDataGridView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Constructor that adds the container to put this BackgroundableDataGridView in.
        /// </summary>
        /// <param name="container">Container to hold the BackgroundableDataGridView</param>
        public BackgroundableDataGridView(IContainer container)
        {
            InitializeComponent();
            container.Add(this);
        }
        #endregion
		/*
        /// <summary>
        /// Overrides the PaintBackground method to allow this View to have a background image.
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="clipBounds"></param>
        /// <param name="gridBounds"></param>
        protected override void PaintBackground(Graphics graphics, Rectangle clipBounds, Rectangle gridBounds)
        {
            base.PaintBackground(graphics, clipBounds, gridBounds);
            TextureBrush tBrush = new TextureBrush(_image);
            try
            {
                graphics.FillRectangle(tBrush, 0, 0, gridBounds.Width, _image.Height);
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                tBrush.Dispose();
            }
        }
		*/
        #region Event Handlers
        /// <summary>
        /// Used to prevent cliping when scrolling vertically.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void verticalScrollBar_valueChanged(object sender, EventArgs e)
        {
            Invalidate();
        }

        /// <summary>
        /// Prevents clipping when scrolling horizontally.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void HorizontalScrollBar_ValueChanged(object sender, EventArgs e)
        {
            Invalidate();
        }

        /// <summary>
        /// Prevents clipping when the size of the View changes.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BackgroundableDataGridView_SizeChanged(object sender, EventArgs e)
        {
            Invalidate();
        }
        #endregion

        /// <summary>
        /// Sets up the BackgroundableDataGridView with the default settings.
        /// </summary>
        private void InitializeComponent()
        {
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
            _image = (Image)(Resource1.strack_bg);
            ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            Location = new System.Drawing.Point(7, 7);
            TabIndex = 2;
            Anchor = AnchorStyles.Bottom |
                AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            ReadOnly = true;
            AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            AllowUserToDeleteRows = false;
            AllowUserToAddRows = false;
            AllowUserToResizeRows = false;
//            DoubleBuffered = true;
			//MultiSelect = false;
            VerticalScrollBar.ValueChanged += new EventHandler(verticalScrollBar_valueChanged);
            HorizontalScrollBar.ValueChanged += new EventHandler(HorizontalScrollBar_ValueChanged);
            this.SizeChanged += new EventHandler(BackgroundableDataGridView_SizeChanged);
        }

        /// <summary>
        /// The Background image for the view.
        /// </summary>
        public Image BackImage
        {
            get { return _image; }
            set { _image = value; }
        }

    }
}
